/* ***** BEGIN LICENSE BLOCK *****
 *   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Save File to.
 *
 * The Initial Developer of the Original Code is
 * Atte Kemppilä.
 * Portions created by the Initial Developer are Copyright (C) 2009
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * 
 * ***** END LICENSE BLOCK ***** */


// http://mxr.mozilla.org/firefox/source/toolkit/content/editMenuOverlay.js
function goUpdateEntryCommands()
{
    for each (var command in EntryController.prototype.commands)
        goUpdateCommand("cmd_" + command);
}


// nsIController
function EntryController()
{
    // empty
}

EntryController.prototype =
{
    commands: "addFolder addOpenPrefs addRecent addSeparator editFolder moveEntriesDown moveEntriesUp removeEntries toggleMenu".split(" "),
    
    get commandSet()
    {
        var prototype = EntryController.prototype;
        var commandSet = new Set(prototype.commands);
        return lazyInit(prototype, "commandSet", commandSet);
    },
    
    get type()     { return prefwindow.type; },
    get enabled()  { return prefwindow.enabled; },
    get treeView() { return prefwindow.treeView; },
    
    isCommandEnabled: function(command)
    {
        if (! (this.type && this.enabled))
            return false;
        
        var treeView = this.treeView;
        var selection = treeView.selection;
        
        switch (command)
        {
            case "cmd_addFolder":
            case "cmd_addOpenPrefs":
            case "cmd_addRecent": 
            case "cmd_addSeparator":
                return true;
                
            case "cmd_editFolder":
                return ((selection.count == 1) && (treeView.selectedEntry.type == "folder"));
                
            case "cmd_moveEntriesDown":
                var indexes = treeView.selectedIndexes;
                return ((selection.getRangeCount() == 1) && (indexes[indexes.length - 1] != (treeView.rowCount - 1)));
                
            case "cmd_moveEntriesUp":
                var indexes = treeView.selectedIndexes;
                return ((selection.getRangeCount() == 1) && (indexes[0] != 0));
                
            case "cmd_removeEntries":
                return (selection.count > 0);
                
            case "cmd_toggleMenu":
                return ((selection.count == 1) && (treeView.selectedEntry.type == "folder"));
                
            default:
                throw new Error("invalid command: " + command);
        }
    },
    
    supportsCommand: function(command)
    {
        // strip "cmd_"
        command = command.substr(4);
        return (command in this.commandSet);
    },
    
    doCommand: function(command)
    {
        if (this.isCommandEnabled(command))
            this[command]();
    },
    
    onEvent: function(eventName) { /* empty */ },
    
    cmd_addFolder: function()
    {
        var entry = {label: "", path: "", menu: true};
        var entry = this._openFolderDialog("add", entry);
        if (! entry)
            return;
        
        entry.type = "folder";
        this._addEntry(entry);
    },
    
    cmd_addOpenPrefs: function()
    {
        var entry = savefileto.getOpenPrefsCommandEntry(this.type);
        this._addEntry(entry);
    },
    
    cmd_addRecent: function()
    {
        this._addEntry(recentEntry);
    },
    
    cmd_addSeparator: function()
    {
        this._addEntry(separatorEntry);
    },
    
    cmd_editFolder: function()
    {
        var index = this.treeView.selectedIndex;
        var entry = this.treeView.selectedEntry;
        
        var folder = this._openFolderDialog("edit", entry);
        if (! folder)
            return;
        
        folder.type = entry.type;
        this.treeView.setEntry(index, folder);
    },
    
    cmd_removeEntries: function()
    {
        if (prefwindow.prefs.getBoolPref("confirmRemove"))
        {
            var title = prefwindow.bundle.getString("remove.title");
            var message = prefwindow.bundle.getString("remove.message");
            var checkLabel = prefwindow.bundle.getString("remove.check");
            var check = {value: false};
            var result = Services.prompt.confirmCheck(window, title, message, checkLabel, check);
            
            if (! result)
                return;
            
            prefwindow.prefs.setBoolPref("confirmRemove", (! check.value));
        }
        
        var indexes = this.treeView.selectedIndexes;
        this.treeView.removeEntries(indexes);
    },
    
    cmd_toggleMenu: function()
    {
        var index = this.treeView.selectedIndex;
        this.treeView.toggleMenu(index);
    },
    
    cmd_moveEntriesDown: function()
    {
        this._moveEntriesBy(1);
    },
    
    cmd_moveEntriesUp: function()
    {
        this._moveEntriesBy(-1);
    },
    
    _addEntry: function(entry)
    {
        var selection = this.treeView.selection;
        
        var index = this.treeView.currentIndex;
        if (index == undefined)
            index = this.treeView.selectedIndex;
        if (index == -1)
            index = this.treeView.rowCount;
        
        this.treeView.addEntries(index, [entry], true);
    },
    
    _moveEntriesBy: function(delta)
    {
        var indexes = this.treeView.selectedIndexes;
        this.treeView.moveEntriesBy(indexes, delta);
    },
    
    _openFolderDialog: function(title, entry)
    {
        var args = {title: title, label: entry.label, path: entry.path, menu: entry.menu};
        var params = {args: args, result: null};
        document.documentElement.openSubDialog("chrome://savefileto/content/prefwindow/folder.xul", null, params);
        return params.result;
    }
}
